CN.record = "个场地";
var msg=CN;
var venue = {edit_status:"create",uid:site.uid};
$(function(){
	init();
	mpage.modalfactory = function(data,position){//生成列表内容 //TODO 需要变化的地方
        $(".tablist").empty();
        tablist.position = position;//设置数据所在位置
        var tdValue =  "",
            rowclass = "",
            index = 1;//当前页面自编序号
            td = function(arg){
            return "<td>"+arg+"</td>";           
            },
            setButton = function(data){
            	return "<a class='btn btn-info btn-mini' onclick='del(this)' data-id='"+data.evid+"' data-name='"+data.name+"' status='0' view='off'>移除</a>";
            };
        for ( var element in data) {
        	var kk =data[element],
             	ids = '<button class="btn btn-info btn-mini" onclick="get(this)" type="button"  data-id="'+kk.evid+'">'+index+'</button>';
             	tdValue =td(ids)+ 
            	td(kk.name)+
            	td(kk.desc)+ 
            	td(kk.url)+
             	td(kk.level)+
             	td(setButton(kk));
               	rowclass = element%2;//行样式
               	index++;
               $(".tablist").append("<tr class='tr"+rowclass+"'>"+tdValue+"</tr>");
        }
    };
    mpage.setext("?eid="+getUrlParam("eid"));
	mpage.init();
}); 

function init(){
	init_dialog();
}

init_dialog = function(){
	$("#dialog_create").click(function(){//create & open dialog
		venue.edit_status = "create";
		open_dialog();
	});
	$("#dialog_save").click(function(){
		$(this).attr("disabled",true);
		$(".dialogmsg").text("执行操作中...");
		venue.name = $("#name").val();
		venue.desc = $("#desc").val();
		venue.url = $("#url").val();
		venue.level = $("#level").val();
		venue.uid = site.uid;
		venue.requesturl = site.web+"my/"+site.title+"/"+venue.edit_status;//包含create/update操作
		venue.eid = getUrlParam("eid");
		venue.evid = $("#evid").val();
		complete = function(data){
			done_method = function(){
				$('#myModal').modal('hide');
				mpage.init();
			};
			if(data.code==1){
				$(".dialogmsg").text("操作成功!");
				setTimeout(done_method, 2000);
				
			}else{
				$('#myModal').modal('hide');
			}
			
		}
		GG(venue,complete);
	});
	$(".canceldialog").click(function(){//cancel
		clear_dialog();
		$('#myModal').modal('hide');
	});
	
}

clear_dialog = function(){
	$("#form1 input[type=reset]").click();
	$("#dialog_save").attr("disabled",false);
}

open_dialog = function(data){
	clear_dialog();
	switch(venue.edit_status){
		case "create":
			$(".dialog_status").text("新增");
			break;
		case "update":
			$(".dialog_status").text("更新");
			$.each(data, function(index, value) {
				if(index !="eid" && index !="w" && index !="h"){
					$("#"+index).val(value);
				}
			});
			break;
	}
	$('#myModal').modal('show');
}

get = function(arg){
	var id = $(arg).attr("data-id");
	cb = function (data){
		if(data.code==1){
			if(data.data.length==1){
				venue.edit_status = "update";
				open_dialog (data.data[0]);
			}
		}else{
			alert("ERROR venue 404");
		}
	}
	var url = site.web+"my/"+site.title+"/get";
	var json = {url:url,evid:id,uid:site.uid};
	G(json,cb);
}

del = function(arg){
	var name =  $(arg).attr("data-name");
	if(confirm("确认移除"+name+"?")==false)return;
	var id = $(arg).attr("data-id");
	cb = function (data){
		mpage.init();
	}
	var url = site.web+"my/"+site.title+"/del";
	var json = {url:url,evid:id,uid:site.uid};
	G(json,cb);
}
